﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight;
using ALiteTeam.PackageTracer.Core;
using GalaSoft.MvvmLight.Command;
using System.Resources;
using System.Windows.Media.Imaging;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.Phone.Scheduler;
using GalaSoft.MvvmLight.Messaging;

namespace ALiteTeam.PackageTracer.ViewModel
{
    public class SettingsViewModel : ViewModelBase
    {
        public static readonly Guid STREAMING_UPDATED = Guid.NewGuid();
        public static readonly Guid ENALBE_AUTOMATIC_UPDATE = Guid.NewGuid();

        public SettingsViewModel()
        {
            if (IsInDesignMode == false)
            {
                this.AllowToast                     =           Settings.Configuration.AllowToast;
                this.AllowUpdateViaBackground       =           Settings.Configuration.AllowUpdateViaBackground;
                this.ThemeColor                     =           Settings.Configuration.ThemeColor;

                BackgroundImages = new ObservableCollection<BitmapImage>();
                //BackgroundImages.Add(new BitmapImage(new Uri(@"/Images/Background/1.jpg", UriKind.Relative)));
                //BackgroundImages.Add(new BitmapImage(new Uri(@"/Images/Background/2.jpg", UriKind.Relative)));
                //BackgroundImages.Add(new BitmapImage(new Uri(@"/Images/Background/3.jpg", UriKind.Relative)));
                //BackgroundImages.Add(new BitmapImage(new Uri(@"/Images/Background/4.jpg", UriKind.Relative)));
                //BackgroundImages.Add(new BitmapImage(new Uri(@"/Images/Background/5.jpg", UriKind.Relative)));
                //BackgroundImages.Add(new BitmapImage(new Uri(@"/Images/Background/6.jpg", UriKind.Relative)));
                //BackgroundImages.Add(new BitmapImage(new Uri(@"/Images/Background/7.jpg", UriKind.Relative)));
                //BackgroundImages.Add(new BitmapImage(new Uri(@"/Images/Background/8.jpg", UriKind.Relative)));
                //BackgroundImages.Add(new BitmapImage(new Uri(@"/Images/Background/9.jpg", UriKind.Relative)));
                //BackgroundImages.Add(new BitmapImage(new Uri(@"/Images/Background/10.jpg", UriKind.Relative)));
                //BackgroundImages.Add(new BitmapImage(new Uri(@"/Images/Background/11.jpg", UriKind.Relative)));
                //BackgroundImages.Add(new BitmapImage(new Uri(@"/Images/Background/12.jpg", UriKind.Relative)));
            }

            this.SaveCommand = new RelayCommand(Save);
            this.SaveAndSwitchCommand = new RelayCommand<bool>(SaveAndSwitch);

            Messenger.Default.Register<bool>(this, ENALBE_AUTOMATIC_UPDATE, SaveAndSwitch);
        }

        public RelayCommand SaveCommand { get; private set; }
        private void Save()
        {
            Settings.Configuration.AllowToast = this.AllowToast;
            Settings.Configuration.AllowUpdateViaBackground = this.AllowUpdateViaBackground;
            Settings.Configuration.ThemeColor = this.ThemeColor;
            //Settings.Configuration.BackgroundImage = this.BackgroundImage;
        }

        public RelayCommand<bool> SaveAndSwitchCommand { get; private set; }
        private void SaveAndSwitch(bool isOn)
        {
            Save();

            if (isOn == true)
            {
                // 开启后台服务
                if (ScheduledActionService.Find("BackgroundRefreshTaskAgent") != null)
                {
                    ScheduledActionService.Remove("BackgroundRefreshTaskAgent");
                }

                PeriodicTask task = new PeriodicTask("BackgroundRefreshTaskAgent");
                task.Description = "存在于后台更新被设定为允许自动更新的订单，并在收到更新信息之后弹出消息提示（可设置关闭）。";
                ScheduledActionService.Add(task);
#if DEBUG
                ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromMinutes(1));
#endif
            }
            else
            {
                if (ScheduledActionService.Find("BackgroundRefreshTaskAgent") != null)
                {
                    ScheduledActionService.Remove("BackgroundRefreshTaskAgent");
                }
            }
        }

        #region 绑定属性区域
        /// <summary>
        /// The <see cref="ThemeColor" /> property's name.
        /// </summary>
        public const string ThemeColorPropertyName = "ThemeColor";
        private ThemeColors mThemeColor = ThemeColors.默认;

        /// <summary>
        /// Gets the ThemeColor property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ThemeColors ThemeColor
        {
            get
            {
                return mThemeColor;
            }

            set
            {
                if (mThemeColor == value)
                {
                    return;
                }
                mThemeColor = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(ThemeColorPropertyName);
            }
        }

        /// <summary>
        /// Gets the StreamingAmount property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public double StreamingAmount
        {
            get
            {
                return Settings.Configuration.NetTraffic/1024;
            }
        }

        /// <summary>
        /// The <see cref="BackgroundImage" /> property's name.
        /// </summary>
        public const string BackgroundImagePropertyName = "BackgroundImages";
        private ObservableCollection<BitmapImage> mBackgroundImages;

        /// <summary>
        /// Gets the BackgroundImage property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ObservableCollection<BitmapImage> BackgroundImages
        {
            get
            {
                return mBackgroundImages;
            }

            set
            {
                if (mBackgroundImages == value)
                {
                    return;
                }
                mBackgroundImages = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(BackgroundImagePropertyName);
            }
        }

        /// <summary>
        /// The <see cref="AllowUpdateViaBackground" /> property's name.
        /// </summary>
        public const string AllowUpdateViaBackgroundPropertyName = "AllowUpdateViaBackground";
        private bool mAllowUpdateViaBackground = false;

        /// <summary>
        /// Gets the AllowUpdateViaBackground property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public bool AllowUpdateViaBackground
        {
            get
            {
                return mAllowUpdateViaBackground;
            }
            set
            {
                if (mAllowUpdateViaBackground == value)
                {
                    return;
                }
                mAllowUpdateViaBackground = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(AllowUpdateViaBackgroundPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="AllowToast" /> property's name.
        /// </summary>
        public const string AllowToastPropertyName = "AllowToast";
        private bool mAllowToast = false;

        /// <summary>
        /// Gets the AllowToast property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public bool AllowToast
        {
            get
            {
                return mAllowToast;
            }

            set
            {
                if (mAllowToast == value)
                {
                    return;
                }

                mAllowToast = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(AllowToastPropertyName);
            }
        }

        #endregion
    }
}
